package com.mashang.stuManSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mashang.stuManSystem.entity.Student;
import com.mashang.stuManSystem.entity.vo.StudentVo;
import com.mashang.stuManSystem.service.IStudentService;
import com.mashang.stuManSystem.utils.R;
import com.mashang.stuManSystem.utils.T1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/student1")
@Api(tags = "学生模块")
public class StudentController {

    @Autowired
    private IStudentService service;
    //通过id查询学生详情
    @GetMapping("/{studentId}")
    @ApiOperation(value = "查询学生详情", notes = "通过学生id查询学生详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId", value = "学生id", required = true, paramType = "path", dataType = "Integer")
    })
    public R<StudentVo> queryById(@PathVariable Integer studentId){
        return R.success(service.queryById(studentId));
    }

    //添加学生
    @PostMapping
    @ApiOperation("添加学生")
    public R<Student> insert(Student student){
        boolean b = false;
        try {
            b = service.save(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.to(b);
    }

    //修改学生
    @PutMapping
    @ApiOperation("修改学生")
    @ApiImplicitParam(
            name = "student",
            value = "学生信息",
            required = true,
            paramType = "body",  // 表示请求体参数
            dataType = "Student"
    )
    public R<Student> update(@RequestBody Student student){
        boolean b = false;
        try {
            b = service.updateById(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.to(b);
    }

    //删除学生
    @DeleteMapping("/{studentId}")
    @ApiOperation("删除学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId", value = "学生id", required = true, paramType = "path")
    })
    public R<Student> delete(@PathVariable Integer studentId){
        return R.to(service.removeById(studentId));
    }

    //查询学生列表
    @GetMapping("/list")
    @ApiOperation("查询学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "学生姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "studentNo", value = "学号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认1）", paramType = "query", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数（默认10）", paramType = "query", dataType = "Integer", defaultValue = "100")
    })
    public T1<List<StudentVo>> queryList(String name,
                                         String studentNo,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        Page<StudentVo> studentVoPage = new Page<>(pageNum, pageSize);
        QueryWrapper<StudentVo> qw = new QueryWrapper<>();
        qw.eq(studentNo != null && !studentNo.equals(""), "student_no", studentNo);
        qw.eq(name != null && !name.equals(""), "t1.name", name);
        Page<StudentVo> studentVoPage1 = service.queryList(studentVoPage, qw);
        List<StudentVo> records = studentVoPage1.getRecords();
        long total = studentVoPage.getTotal();
        return T1.success(records, total);
    }
}
